//
//  CZWelcomeViewController.swift
//  微博08
//
//  Created by 1 on 16/3/5.
//  Copyright © 2016年 cjf. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD
import SnapKit

class CZWelcomeViewController: UIViewController {

    // MARK: - 属性
    // 头像的底部约束
    var iconImageViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        // 先设置头像
        setIcon()
        // 获取用户头像
        CZUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("控制器error: \(error)")
                return
            }
            //设置用户头像
            self.setIcon()
        }
    }
    
    //设置用户头像
    private func setIcon() {
        if let avatar_large = CZUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.iconImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
    
        // 1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconImageView)
        view.addSubview(messageLabel)
        
        // 2.添加约束
        // 背景
        // VFL: 可视化
        /*
        format:
            H: 水平方向约束   |父控件的左边    |父控件的右边
            V: 垂直方向约束
        */
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
        bkgImageView.snp_makeConstraints { (make) -> Void in
            // make: 哪个view调用的就是哪个view
            // 填充父控件
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        iconImageView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(view.snp_centerX)
            // 当约束view的属性和参照view的属性相同的时候，参照的属性可以省略
            make.centerX.equalTo(view)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // messageLabel
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
    }
    
    // 动画
    private func moveAnimation() {
        // 改变头像底部的约束
//        iconImageViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        // 更新iconImageView的底部约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            // 要更新的约束
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
    
        // 动画
        /*
        duration: NSTimeInterval    时间
        delay: NSTimeInterval       延迟
        usingSpringWithDamping: CGFloat  弹簧的明显程度 0-1
        initialSpringVelocity: CGFloat   初始速度
        animations: 更新动画
        completion: 完成的闭包
        */
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // label渐变动画
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 跳转到CZMainViewController
                        AppDelegate.outSwitchRootViewController(true)
                })
        }
    }
    
    // MARK: - 懒加载
    // 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    // label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        // 设置
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()
}
